package ita.class1.group2.supermarket.administrator.controller;


import ita.class1.group2.supermarket.administrator.pojo.*;
import ita.class1.group2.supermarket.administrator.service.*;
import ita.class1.group2.supermarket.common.util.RespMessageFactory;
import ita.class1.group2.supermarket.common.annotation.Autowired;
import ita.class1.group2.supermarket.common.annotation.Component;
import ita.class1.group2.supermarket.common.annotation.RequestMapping;
import ita.class1.group2.supermarket.common.auth.SecurityUtils;
import ita.class1.group2.supermarket.common.pojo.Page;
import ita.class1.group2.supermarket.common.pojo.ResponseMessage;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class AdminController {
    @Autowired
    private StoreFormService storeFormService;
    @Autowired
    private StoreFormRecordService recordService;
    @Autowired
    private AdFormService adFormService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private AdminService adminService;
    @Autowired
    SecurityUtils securityUtils;

    @RequestMapping("/admin/orders")
    public ResponseMessage storeRecords() {
        List<StoreForm> records = storeFormService.getStoreForms();
        return RespMessageFactory.createSuccessMessage(records);
    }

    /**
     * 查询指定状态的审核单
     *
     * @param map
     * @return
     */
    @RequestMapping("/admin/ordersByStatus")
    public ResponseMessage getRecordsByStatus(Map<String, String> map) {
        String status = map.get("status");
        Integer page = Integer.parseInt(map.get("page"));
        return RespMessageFactory.createSuccessMessage(storeFormService.getStoreFormByStatus(status,page,6));
    }

    /**
     * 修改审核单
     *
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/admin/order/update")
    public ResponseMessage modifyStatus(Map<String, String> map) {
        String srid = map.get("id");
        String status = map.get("status");
        Integer page = Integer.parseInt(map.get("page"));
        recordService.updateStatus(srid, status);
        return RespMessageFactory.createSuccessMessage(storeFormService.getStoreFormByStatus("0",page,6));
    }


    @RequestMapping("/admin/ads")
    public ResponseMessage getAds(Map<String, String> map) {
        Integer page = Integer.parseInt(map.get("page"));
        List<AdStore> adStores = new ArrayList<AdStore>();
        Page<AdForm> allAd = adFormService.getAdByPage(page,22);
        for (AdForm ad : allAd.getItems()) {
            User user = adminService.getUser(ad.getConsumeUId());
            String sid = ad.getsId();
            Store store = storeService.getStoreById(sid);
            if (store != null && store.getStatus() == Store.AVAILABLE) {
                AdStore adStore = new AdStore(ad, store, user);
                adStores.add(adStore);
            }
        }
        return RespMessageFactory.createSuccessMessage(new Page<AdStore>(page,22,adStores,allAd.getItemTotal()));

    }


    @RequestMapping("/admin/ad/confirm")
    public ResponseMessage confirmAd(Map<String, String> map) throws ServletException, IOException {
        User admin = (User) securityUtils.getSubject().getAuthorizingInfo();
        String id = map.get("id");
        adFormService.updateAd(id, admin.getId());
        return getAds(map);
    }
}
